package yxy.game.pm2.active.command.supfirst;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_超值首冲;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PayMessage;
import yxy.game.pm2.message.PayMessage.SupFirstDrawRewardRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

@MessageType(proto = Command.SupFirstDrawReward, classtype = PayMessage.SupFirstDrawRewardRequest.class)
public class CMD_Active_超值首充_DrawReward extends Command<SupFirstDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, SupFirstDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int groupid = request.getSfid();

		PayMessage.SupFirstDrawRewardResponse.Builder response = PayMessage.SupFirstDrawRewardResponse.newBuilder();

		ActiveModule_超值首冲 am = ActiveModule_超值首冲.get(pcode,false);
		if (am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if (!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if (!am.cangainreward(groupid)) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}

		ArrayList<Currency> curs = am.gainreward(pcode, groupid);
		if (curs == null) {
			send(messageSource, response, Result.DataError);
			return;
		}

		new CurrencyHandle().addCurrency(pcode, CCReason.活动_超值首充, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PayMessage.SupFirstDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
